package com.xying.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.xying.common.ServerResponse;
import com.xying.dao.AttendanceMapper;
import com.xying.dao.UsrMapper;
import com.xying.pojo.Attendance;
import com.xying.pojo.Usr;
import com.xying.service.AttendanceService;
import com.xying.util.DateTimeUtil;
import com.xying.vo.AttendanceView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2021/4/26.
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {


    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private UsrMapper usrMapper;


    @Override
    public ServerResponse<PageInfo> getAttendances(String idNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        //查找用户所有的考勤记录
        List<Attendance> attendanceList = attendanceMapper.selectByPrimaryKey(idNo);
        if (attendanceList.size() == 0) {
            return ServerResponse.createByErrorMessage("查找失败或者没有考勤记录");
        }

        Usr usr = usrMapper.selectByPrimaryKey(idNo);

        //调整view
        List<AttendanceView> attendanceViewList = Lists.newArrayList();
        for (Attendance attendance : attendanceList){
            AttendanceView attendanceView = new AttendanceView();
            String dateString = DateTimeUtil.dateToString(attendance.getCreateTime());

            attendanceView.setIdNo(attendance.getIdNo());
            attendanceView.setCreateTime(dateString);
            attendanceView.setUpdateTime(dateString);
            attendanceView.setUsername(usr.getUserName());
            attendanceView.setStatus(attendance.getStatus());

            attendanceViewList.add(attendanceView);
        }

        PageInfo pageInfo = new PageInfo(attendanceList);
        pageInfo.setList(attendanceViewList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse createAttendance(String idNo) {
        Attendance attendance = new Attendance();
        Date date = new Date();
        attendance.setIdNo(idNo);
        attendance.setStatus(true);
        attendance.setUpdateTime(date);
        attendance.setCreateTime(date);
        int rowCount = attendanceMapper.insert(attendance);
        if (rowCount <= 0){
            return ServerResponse.createByErrorMessage("打卡失败");
        }

        String username = usrMapper.selectUserNameByIdNo(idNo);
        return ServerResponse.createBySuccess(username);
    }

    @Override
    public ServerResponse updateAttendance(String idNo, int status, String createTime) {
        int rowCount = attendanceMapper.updateByIdAndTime(idNo, status==1 ? true : false, createTime, new Date());
        if (rowCount > 0){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByError();
    }
}
